package cnsukidayo.com.gitee.controller.user;

import cnsukidayo.com.gitee.model.params.*;
import cnsukidayo.com.gitee.model.pojo.UserContract;
import cnsukidayo.com.gitee.model.support.BaseResponse;
import cnsukidayo.com.gitee.service.UserService;
import cnsukidayo.com.gitee.utils.UserContextUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author sukidayo
 * @date: 2023/3/13 14:23
 */
@RestController
@RequestMapping("/user/user")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation("用户登陆")
    @PostMapping(value = "login")
    public BaseResponse<String> login(@RequestBody @Validated LoginParam loginParam, HttpServletResponse httpServletResponse) {
        userService.login(loginParam, httpServletResponse);
        return BaseResponse.ok("登陆成功!");
    }

    @ApiOperation("根据token获取当前用户信息")
    @GetMapping("getUser")
    public UserContract getUser(HttpServletRequest httpServletRequest) {
        return userService.getUserInfo(UserContextUtils.getToken(httpServletRequest));
    }

    @ApiOperation("收养宠物")
    @PostMapping("adoptPet")
    public void adoptPet(@RequestBody @Validated AdoptPetParam adoptPetParam, HttpServletRequest httpServletRequest) {
        userService.adoptPet(UserContextUtils.getToken(httpServletRequest), adoptPetParam);
    }

    @ApiOperation("用户参与活动")
    @PostMapping("participateEvent")
    public void participateEvent(@RequestBody @Validated ParticipateEventParam participateEventParam, HttpServletRequest httpServletRequest) {
        userService.participateEvent(UserContextUtils.getToken(httpServletRequest), participateEventParam);
    }

    @ApiOperation("积分兑换")
    @PostMapping("exchangePoints")
    public void exchangePoints(@RequestBody @Validated ExchangePointsParam exchangePointsParam, HttpServletRequest httpServletRequest) {
        userService.exchangePoints(UserContextUtils.getToken(httpServletRequest), exchangePointsParam);
    }


}
